﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace SecuritiesAnalysis.Common
{
    public static class Log
    {
        private static object lockobj=new object();

        private static void WriteToFile(string filename, string content)
        {
            using (StreamWriter sw = new StreamWriter(filename, true))
            {
                sw.WriteLine(content);
                sw.Close();
            }
        }

        private static string GetDateTime()
        {
            return "[" + DateTime.Now.ToString("HH:mm:ss") + "] ";
        }

        public static void Output(string msg)
        {
            lock (lockobj)
            {
                Log.WriteToFile(Directory.GetCurrentDirectory()+ @"\log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt", GetDateTime() + msg);
            }
        }

        public static void OutputError(string msg)
        {
            lock (lockobj)
            {
                StackFrame sf = new StackTrace(true).GetFrame(1);
                StringBuilder sb = new StringBuilder(GetDateTime());
                sb.Append("ERROR:");
                sb.AppendLine(msg);
                sb.AppendLine("Error from:");
                sb.AppendLine(sf.GetFileName());
                sb.Append("Line:");
                sb.AppendLine(sf.GetFileLineNumber().ToString());
                sb.Append("Column:");
                sb.AppendLine(sf.GetFileColumnNumber().ToString());
                sb.Append("Method:");
                sb.Append(sf.GetMethod().Name);
                Log.WriteToFile(Directory.GetCurrentDirectory() + @"\log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt",  sb.ToString());
            }
        }
    }
}
